package sw6.digipecs.admin;

import sw6.digipecs.R;
import sw6.digipecs.exceptions.DatabaseException;
import sw6.digipecs.model.Situation;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class CreateSituationActivity extends Activity implements OnClickListener {
	public static final int CREATE_SITUATION_REQUEST_CODE= 1; 
	
	private Button mButtonSave;
	private EditText mEditName;
	private Button mButtonCancel;
	private Context mContext;

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.createsituationmainlayout); 
		initialise(); 
	}

	private void initialise() {
		mButtonSave = (Button) findViewById(R.id.createSituationOkButton);
		mButtonSave.setOnClickListener(this); 
		mButtonCancel = (Button)findViewById(R.id.createSitatuonCancelButton); 
		mButtonCancel.setOnClickListener(this); 
		mEditName = (EditText) findViewById(R.id.createSituationNameEditText);
		mContext = getApplicationContext(); 
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.createSituationOkButton:
			CreateSituation(); 
			break;
		case R.id.createSitatuonCancelButton:
			this.setResult(RESULT_CANCELED); 
			this.finish(); 
			break;
		}
	}

	private void CreateSituation() {
		try {
			Situation.CreateNew(mContext, (mEditName.getText()).toString());
		} catch (DatabaseException e) {
			e.printStackTrace();
		} 
		this.setResult(RESULT_OK); 
		this.finish(); 
	}
}
